<?php

/**
 * @author Lim Yi Le
 */
class libraryupdates_model extends CI_Model {

    var $title = '';
    var $fullContent = '';
    var $pubDate = '';
    var $summary = false;
    var $summaryContent = '';

    function __construct() {
        parent::__construct();
    }

/*
|--------------------------------------------------------------------------
| parseLibraryUpdatesXML (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function parses the library updates RSS feeds as XML and formats the HTML5
| size of the <img> tag and forces <a href> tags to open in a new window. Do take note
| that the RSS feed for the default 'All' category is in a different XML format and
| has different element names and thus is parsed separately. Both the summarized and
| full content of each library updates post is captured.
|
*/ 
    
    function parseLibraryUpdatesXML($feed, $category, $contentSize){        
        $libraryUpdates = array();
        $eachPost = $feed->entry;
        
        if($category == 'All'){
            $eachPost = $feed->channel->item;
        }
        
        foreach($eachPost as $item){
            $libraryUpdate = new LibraryUpdates_Model();
            $libraryUpdate->title = (string)$item->title;
            $libraryUpdate->pubDate = (string)$item->published;
            $libraryUpdate->fullContent = (string)$item->content;
            
            if($category == 'All'){
                $libraryUpdate->pubDate = (string)$item->pubDate;
                $libraryUpdate->fullContent = (string)$item->description;
            }

            // open in new window for <a href> tags
            $regularExpOpenLinkInNewWindow = '/imageanchor="[0-9]*"/';
            $libraryUpdate->fullContent = preg_replace($regularExpOpenLinkInNewWindow, 'imageanchor="1" target="_blank"', $libraryUpdate->fullContent);
            
            // remove width and height for <img> tags 
            $regularExpRemoveWidth = '/width="[0-9]*"/';
            $libraryUpdate->fullContent = preg_replace($regularExpRemoveWidth, '', $libraryUpdate->fullContent);
            
            $regularExpRemoveHeight = '/height="[0-9]*"/';
            $libraryUpdate->fullContent = preg_replace($regularExpRemoveHeight, '', $libraryUpdate->fullContent);
            
            // adds in dynamic width and height for <img> tags
            $regularExpInsert = '/border="[0-9]*"/';
            $libraryUpdate->fullContent = preg_replace($regularExpInsert, 'border="0" width="70%" height="70%"', $libraryUpdate->fullContent);
            
            // truncates content for Summary of library updates post
            if($contentSize == 'Summary'){
                $libraryUpdate->summaryContent = $this->myTruncate($libraryUpdate, $libraryUpdate->fullContent, 800);
            }
            
            $libraryUpdates[] = array('title' => $libraryUpdate->title,
                'pubDate' => $libraryUpdate->pubDate,
                'summaryContent' => $libraryUpdate->summaryContent,
                'summary' => $libraryUpdate->summary,
                'fullContent' => $libraryUpdate->fullContent);
        }
        
        $libraryUpdatesList = array('libraryUpdates' => $libraryUpdates);
        return $libraryUpdatesList;
    }

/*
|--------------------------------------------------------------------------
| myTruncate (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function truncates the string of the content tag for the summary view of
| library updates, adding a "..." pad if the length of the string is over the limit defined.
|
*/     

    function myTruncate($libraryUpdate, $string, $limit, $break="<", $pad="..."){
        // return with no change if string is shorter than $limit
        if(strlen($string) <= $limit){
            $newString = $string;
        }
        // is $break present between $limit and the end of the string?
        else if(false !== ($breakpoint = strpos($string, $break, $limit))) {
            $libraryUpdate->summary = true;
            if($breakpoint < strlen($string) - 1) {
                $newString = substr($string, 0, $breakpoint) . $pad;
            }
        }
        return $newString;
    }
}

?>
